package sd.czw.com.mmgapp.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cjj.MaterialRefreshLayout;
import com.cjj.MaterialRefreshListener;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import sd.czw.com.mmgapp.Contants;
import sd.czw.com.mmgapp.R;
import sd.czw.com.mmgapp.adapter.CategoryGoodsAdapter;
import sd.czw.com.mmgapp.mcallback.CatageoryGoodsCallback;
import sd.czw.com.mmgapp.model.categorybean.CategoryFragmenGrideViewtInfo;

/**
 * Created by czw
 * desc 描述：
 * on 2017/7/7.
 */
public class TabGoodsFragment extends Fragment {

    private View rootviews;
    private String id;
    private List<CategoryFragmenGrideViewtInfo.ErrmsgBean.CatGoodsBean> gdatas = new ArrayList<>();
    private List<CategoryFragmenGrideViewtInfo.ErrmsgBean.CatGoodsBean> cat_goods;
    private String img;
    private CategoryGoodsAdapter categoryGoodsAdapter1;
    private int width;
    private MaterialRefreshLayout refresh;
    private boolean isLoadMore = true;
    private int start = 0;
    public static final int limit = 20;
    private int page = 0;
    private RecyclerView rv;
    private String path;

    public static TabGoodsFragment newInstance(String id,String path) {
        Bundle args = new Bundle();
        args.putString("id", id);
        args.putString("path",path);
        TabGoodsFragment fragment = new TabGoodsFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootviews = inflater.inflate(R.layout.recycleview, container, false);

        rv = (RecyclerView) rootviews.findViewById(R.id.rv);
        id = getArguments().getString("id");
        path = getArguments().getString("path");
        width = getActivity().getWindowManager().getDefaultDisplay().getWidth();
        getdatas(start);
        refreshandloadmore();
        GridLayoutManager glm = new GridLayoutManager(getContext(),2,LinearLayoutManager.VERTICAL,false);
        rv.setLayoutManager(glm);
        categoryGoodsAdapter1 = new CategoryGoodsAdapter(getContext(),width ,path,gdatas);
        rv.setHasFixedSize(true);
        rv.setNestedScrollingEnabled(false);
        rv.setAdapter(categoryGoodsAdapter1);
        return rootviews;
    }

    /**
     * 刷新、加载更多
     */
    private void refreshandloadmore() {
        refresh = (MaterialRefreshLayout) rootviews.findViewById(R.id.refresh);
        refresh.setSunStyle(true);
        refresh.setIsOverLay(true);
        refresh.setWaveShow(false);
        refresh.setLoadMore(isLoadMore);
        refresh.setMaterialRefreshListener(new MaterialRefreshListener() {
            @Override
            public void onRefresh(MaterialRefreshLayout materialRefreshLayout) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        start = 0;
                        gdatas.clear();
                        getdatas(start);
                        categoryGoodsAdapter1.notifyDataSetChanged();
                        refresh.finishRefresh();
                    }
                },1000);
            }

            @Override
            public void onRefreshLoadMore(MaterialRefreshLayout materialRefreshLayout) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        isLoadMore = false;
                        page++;
                        start = 20*page;
                        getdatas(start);
                        refresh.finishRefreshLoadMore();
                    }
                }, 500);
            }
        });
        //自动刷新
        if (gdatas != null){
            refresh.autoRefresh();
        }
    }
    private void getdatas(int s) {
        OkHttpUtils.post().url(Contants.FL_GOODS)
                .addParams("time",Contants.Time)
                .addParams("sign",Contants.Sign)
                .addParams("act","cat_goods")
                .addParams("cat_id",id)
                .addParams("start", String.valueOf(s))
                .addParams("limit", String.valueOf(limit))
                .build().execute(new CatageoryGoodsCallback() {
            public CategoryFragmenGrideViewtInfo.ErrmsgBean errmsg;

            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(CategoryFragmenGrideViewtInfo response, int id) {
                if (response.getErrcode() == 1){
                    errmsg = response.getErrmsg();
                    cat_goods = errmsg.getCat_goods();
                    gdatas.addAll(cat_goods);
                    categoryGoodsAdapter1.notifyDataSetChanged();
                }
            }
        });
    }
}
